using System;

namespace codebase.UI
{
    public class RadioButton : Control
    {
        public delegate void SelectedIndexChangedDelegate(int idx);

        public event SelectedIndexChangedDelegate SelectedIndexChanged;

        internal void DoIndexChanged(int idx, string lbl)
        {
            if (SelectedIndexChanged != null) SelectedIndexChanged(idx);
            if (SelectedButtonChanged != null) SelectedButtonChanged(lbl);
        }

        public delegate void SelectedButtonChangedDelegate(String lbl);

        public event SelectedButtonChangedDelegate SelectedButtonChanged;

        public int ForeColor;
        public int ButtonColor;

        public string[] Items;
        public int Index;

        public RadioButton(int x, int y, string[] lbls, int tc, int bgc, int bc, Window parent)
        {
            Init(x, y, lbls, tc, bgc, bc, parent);
        }

        void Init(int x, int y, string[] lbls, int tc, int bgc, int bc, Window parent)
        {
            Parent = parent;

            Left = x * Parent.Parent.screen.CharacterWidth;
            Top = y * Parent.Parent.screen.CharacterHeight;
            Height = lbls.Length * Parent.Parent.screen.CharacterHeight;
            Items = lbls;
            Index = 0;
            BackgroundColor = bgc;
            ForeColor = tc;
            ButtonColor = bc;

            updateWidth();
        }

        public void updateLabels(string[] lbls)
        {
            Items = lbls;
            Index = 0;
            updateWidth();
            Height = lbls.Length * Parent.Parent.screen.CharacterHeight;
        }

        void updateWidth()
        {
            int max_len = 0;
            foreach (string s in Items)
            {
                if (s.Length > max_len)
                    max_len = s.Length;
            }
            max_len += 1;
            Width = max_len * Parent.Parent.screen.CharacterWidth;
        }

        protected override void DrawAll()
        {
            int i;
            
            for (i = 0; i < Items.Length; i++)
            {
                Parent.Parent.screen.DrawText(Items[i], Left + Parent.Left + Parent.Parent.screen.CharacterWidth, Top + Parent.Top + i * Parent.Parent.screen.CharacterHeight, false, BackgroundColor, ForeColor);
            }

            Draw();
        }

#if !PC
        public override void touchDown(object o, TouchEventArgs e)
        {
            int idx;
            if (IsPointInControl(e.x, e.y))
            {
                int tx = e.y - Top - Parent.Top;
                idx = tx / Parent.Parent.screen.CharacterHeight;
                if (idx != Index)
                {
                    Index = idx;
                    Draw();
                    DoIndexChanged(Index, Items[Index]);
                }
            }
        }
#endif

        protected override void Draw()
        {
            int i;

            for (i = 0; i < Items.Length; i++)
            {
                int ipos = i * Parent.Parent.screen.CharacterHeight;
                Parent.Parent.screen.DrawFilledRectangle(Left + Parent.Left, Top + Parent.Top + ipos , Left + Parent.Left + Parent.Parent.screen.CharacterWidth - 1, Top + Parent.Top + ipos + Parent.Parent.screen.CharacterHeight - 1, BackgroundColor);
                if (i == Index)
                {
                    Parent.Parent.screen.DrawFilledCircle(Left + Parent.Left + 3, Top + Parent.Top + ipos + 3, 3, ButtonColor);
                }
                Parent.Parent.screen.DrawCircle(Left + Parent.Left+ 3, Top + Parent.Top + ipos + 3, 3, ForeColor);
            }
        }
    }
}
